package com.itheima.d5_collection_test.test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.TreeSet;

public class Room {
    private ArrayList<Card> allcards = new ArrayList<>();

    //无参构造器
    public Room() {
        //1、生成牌
        String[] colors = {"♥️","♠️","♦️","♣️"};
        String[] number = {"3", "4", "5", "6", "7", "8", "9","10","J", "Q", "K", "A", "2"};
        int size = 0; //记录每张牌的大小，意义在于后面方便牌的排序

        for (String num : number) {
            for (String color : colors) { //不可先遍历colors再遍历number ---> 不然后面牌的大小不方便计算
//                System.out.println(color + num);
                //生成牌，放到集合中
                allcards.add(new Card(num,color,size++));
//                size++;
            }
        }
        //大小王
        allcards.add(new Card("小🃏", "", size++));
        allcards.add(new Card("大🃏", "", size++));

//        //测试打印
//        allcards.forEach(System.out::println);
    }

    public void start() {
        //2、洗牌
        Collections.shuffle(allcards);
//        allcards.forEach(System.out::println);

        //3、发牌
        //使用TreeSet，目的：发牌后自动排序
        TreeSet<Card> player1 = new TreeSet<>();
        TreeSet<Card> player2 = new TreeSet<>();
        TreeSet<Card> player3 = new TreeSet<>();
        TreeSet<Card> dipai = new TreeSet<>();

        //遍历allCards集合进行发牌
        for (int i = 0; i < allcards.size(); i++) {
            //发的牌
            Card card = allcards.get(i);
            //先把底牌排除
            if (i > 50) {
                //底牌
                dipai.add(card);
            } else if (i % 3 == 0) {
                player1.add(card);
            }else if (i % 3 == 1) {
                player2.add(card);
            }else if(i % 3 == 2){
                player3.add(card);
            }
        }


        //4、排序
        //TreeSet具有自动排序的能力


        //5、看牌
        System.out.println("赌神：" + player1);
        System.out.println("赌圣：" + player2);
        System.out.println("赌侠：" + player3);
        System.out.println("底牌：" + dipai);

        //6、模拟随机抢地主
        Random r = new Random();
        int who = r.nextInt(3);
        switch (who) {
            case 0 :
                System.out.println("赌神抢地主成功～");
                player1.addAll(dipai);
                System.out.println("赌神的牌：" + player1);
                break;
            case 1 :
                System.out.println("赌圣抢地主成功～");
                System.out.println("赌圣的牌：" + player2);
                player2.addAll(dipai);
                break;
            case 2 :
                System.out.println("赌侠抢地主成功～");
                System.out.println("赌侠的牌：" + player3);
                player3.addAll(dipai);
                break;
        }
    }
}
